var useridn = '';
function logoutnow(){
    layui.layer.confirm('确定要退出登录吗？', {
        btn: ['是','否'] 
    }, function(){
        sessionStorage.removeItem("exam_userid_s");
        sessionStorage.removeItem("exam_username_s");
        location.href = "juslr/logout.php";
    });
}
var userid = sessionStorage.getItem("exam_userid_s");
if(userid === null || userid === ''){
    userid = get_userid_2();
}
sessionStorage.setItem("exam_userid_forget_s",userid);
var examtion_putplace = document.getElementById('examtion_putpl');

function getallexamtion(){
    fetch('./exam/getall.php?userid='+userid)
    .then(function (response) {
        return response.json();
    }).then(function (data) {
        if(data.code === 200){
            yhload.loadqr_hide('examtion_putpl');
            data_result = data.result;
            data_result.forEach(godin => {
                const table = document.createElement('table');
                var headerRow = document.createElement('tr');
                ['答题人', '试卷ID', '分数','是否作弊', '操作'].forEach(function(text) {
                    var th = document.createElement('th');
                    th.textContent = text;
                    headerRow.appendChild(th);
                });
            table.appendChild(headerRow);
            var dataRow = document.createElement('tr');
            if(godin.ifzb === 1){
                var exmatztss = '作弊';
            }else{
                var exmatztss = '未作弊';
            }
            if(godin.score === null || godin.score === ''){
                var exmatscore = '批阅中';
            }else{
                var score = godin.score;
                var exmatscore = score.split(',')[0];
            }
            [godin.aname, godin.lisid, exmatscore,exmatztss, 
                `<a onclick="survey_seeino('${godin.lisid}');">查看</a>`
            ].forEach(function(textOrHtml, index) {
            var td = document.createElement('td');
            if (index === 4) {
                td.innerHTML = textOrHtml;
            } else {
                td.textContent = textOrHtml;
            }
            dataRow.appendChild(td);
});
            table.appendChild(dataRow);
            table.id = godin.lisid;
            examtion_putplace.appendChild(table);
            table.classList.add('slide-in-from-right');
            });
        }else{
            Qmsg.error('异常的模块处理');
        }
    });
}
function isPageLoaded() {
    return document.readyState == "complete";
}


if (!isPageLoaded()) {
    window.onload = function () {
        yhload.loadqr_show('examtion_putpl');
        getallexamtion();
    }
}

function survey_seeino(searchid){
    window.open(`/share-exams/score-exam/?${searchid}`);    
}



function vpnewexam(){
    layer.open({
        type: 2,
        area: ['871px', '500px'],
        title: '绑定Teacher',
        content: '../plugin/others/bind.php?mno=bd'
    });
}
function msgtoknow(){
    layer.open({
        type: 2,
        area: ['871px', '500px'],
        title: '消息中心',
        content: '../plugin/others/msg.php?type=s&userid='+userid
    });
}

function newgrname_update(newname){
    fetch('./status/status.php?newname='+newname+'&userid='+userid)
    .then(function (response) {
        return response.json();
    }).then(function (data) {
        layer.closeAll();
        if(data.status === 200){
            Qmsg.success('修改成功');
        }else{
            Qmsg.error('修改失败');
        }
    });
}
document.getElementById('usertoonclick').addEventListener('click',function(){
    layer.confirm('确定要修改名称吗(已实名则无法修改)', {
        btn: ['是', '否']
    }, function(){
        if(document.getElementById('smzt_namereal').innerText === '已实名认证'){
            layer.alert('已实名认证，无法修改');
            return;
        }
        layer.prompt({
            formType: 2,
            value: '',
            title: '输入新的用户名'
        }, function(newname, index){
            newgrname_update(newname);
            layer.closeAll();
        });
    });
    });

    function open_realhumantoauth(){
        layer.open({
            type: 2,
            closeBtn: 0
            ,title: '实名认证'
            ,area: ['600px', '500px']
            ,content: '/plugin/others/realhumantoauth2.php?userid='+userid
        });
    }
    function loadingaboutmyself2(){
        fetch('./exam/getallbym.php?userid='+userid)
        .then(function (response) {
            return response.json();
        }).then(function (data) {
            if(data.code === 200){
                var resultvip = data.result;
                var facetoauth = ifopenauthface();
                const kzts = document.getElementById('smzt_namereal');
                sessionStorage.setItem('exam_username_forget_s',resultvip[0].name)
                if(facetoauth === 'open'){
                    if(resultvip[0].smrz === null || resultvip[0].smrz === ''){
                        layer.open({
                            closeBtn: 0,
                            content: '请先进行实名认证'
                            ,btn: ['立即认证','退出登录']
                            ,yes: function(index, layero){
                                layer.closeAll();
                                open_realhumantoauth();
                            }
                            ,btn2: function(index, layero){
                                logoutnow();
                            }
                        });
                    }else{
                        kzts.innerText = '已实名认证';
                    kzts.style = '';
                    kzts.onclick = function(){
                        layer.alert('若要修改实名认证信息，请联系管理员');
                    }
                    }
                }
            }else{
                Qmsg.error('异常的模块处理');
            }})
    }
    loadingaboutmyself2();